//
//  eiaHtmlInterperter.m
//  MarketDataColloctor
//
//  Created by jiro on 10/10/17.
//  Copyright 2010 jiro music. All rights reserved.
//

#import "eiaHtmlParser.h"


@implementation EiaHtmlParser

+ (BOOL) parseDataHtml: (NSString*) html dataInterval: (DataInterval) interval latestDate: (NSDate*) latestDate result: (NSMutableDictionary*) results;
{
	NSError* error = nil;
	NSXMLDocument* document = [ [ NSXMLDocument alloc ] initWithXMLString: html options:NSXMLDocumentTidyHTML error: & error ];
	if ( document == nil ) {
		NSLog( @"xml parse error: %@", document, error );
		return NO;
	}
	
	NSXMLElement* element = [ document rootElement ];
	
	switch ( interval ) {
	case Daily:
		return [ EiaHtmlParser parseDailyDataHtml: element latestDate: latestDate result: results ];
	case Weekly:
		return [ EiaHtmlParser parseWeeklyDataHtml: element latestDate: latestDate result: results ];

	default:
		return NO;
	}
}

+ (BOOL) parseWeeklyDataHtml: (NSXMLElement*) element latestDate: (NSDate*) latestDate result: (NSMutableDictionary*) results
{
	id error;
	id nodes = [ element nodesForXPath: @"/html/body/form/table[3]/tbody/tr"  error: & error ];	
//	NSLog( @"%d", [ nodes count ] );
//	for ( int i = 0; i < [ nodes count ]; i++ ) {
//		NSLog( @"%@", [ nodes objectAtIndex: i ] );
//	}

	id node;
	NSEnumerator* e = [ nodes objectEnumerator ];
	while ( node = [ e nextObject ] ) {

		// get year
		id nodes = [ node nodesForXPath: @"td[1]" error: & error ];
		id processingNode = [ nodes objectAtIndex: 0 ];
		id year = [ processingNode stringValue ];
		if ( [ year length ] <= 4 ) {
			continue;
		}
		year = [ year stringByTrimmingCharactersInSet: [ NSCharacterSet whitespaceAndNewlineCharacterSet ] ];
		year = [ year substringToIndex: 4 ];

		int count = [ node childCount ] / 2;
		for ( int i = 0; i < count; i++ ) {
		
	//		NSLog( @"%@", nodes );
			
			nodes = [ node nodesForXPath: [ NSString stringWithFormat: @"td[%d]", 2 * i + 2 ]  error: & error ];
			if ( [ nodes count ] == 0 ) {
				break;
			}
			processingNode = [ nodes objectAtIndex: 0 ];
	//		NSLog( @"%@", processingNode );
			id date = [ processingNode stringValue ];
			date = [ date stringByTrimmingCharactersInSet: [ NSCharacterSet whitespaceAndNewlineCharacterSet ] ];
			if ( [ date isEqualToString: @"" ] ) {
				continue;
			}
			date = [ date stringByReplacingOccurrencesOfString: @"/" withString: @"-" ];

			NSDate* ns_date = [ NSDate dateWithNaturalLanguageString: [ NSString stringWithFormat: @"%@-%@", year, date ] ];
			if ( latestDate != nil && [ ns_date timeIntervalSinceDate: latestDate ] <= 0 ) {
				continue;
			}

			nodes = [ node nodesForXPath: [ NSString stringWithFormat: @"td[%d]", 2 * i + 3 ] error: & error ];
			processingNode = [ nodes objectAtIndex: 0 ]; 
			id value = [ processingNode stringValue ] ;
			value = [ value stringByTrimmingCharactersInSet: [ NSCharacterSet whitespaceAndNewlineCharacterSet ] ];
			value = [ value stringByReplacingOccurrencesOfString: @"," withString: @"" ];
//			value = [ value removeCharactersInString: @"," ];

//			NSLog( @"year: %@, date: %@, value: %@", year, date, value );

			[ results setObject: value forKey: ns_date ];
		}
	}
	return YES;
}

+ (BOOL) parseDailyDataHtml: (NSXMLElement*) element latestDate: (NSDate*) latestDate result: (NSMutableDictionary*) results
{
	id error;
	id nodes = [ element nodesForXPath: @"/html/body/form/table[4]/tr"  error: & error ];
//	NSLog( @"%@", element );
//	NSLog( @"%d", [ nodes count ] );
	for ( int i = 0; i < [ nodes count ]; i++ ) {

//		NSLog( @"%@", [ nodes objectAtIndex: i ] );
		error = nil;

		id node = [ nodes objectAtIndex: i ];

		id thisNodes = [ node nodesForXPath: @"td[@class=\"B6\"]" error: & error ];
		if ( error != nil ) {
			NSLog( @"%@", error );
			[ results removeAllObjects ];
			return NO;
		}
		if ( [ thisNodes count ] == 0 ) { 
			continue;
		}
		id thisNode = [ thisNodes objectAtIndex: 0 ];
		id dateText = [ thisNode stringValue ]; 
		dateText = [ dateText stringByTrimmingCharactersInSet: [ NSCharacterSet whitespaceAndNewlineCharacterSet ] ];
//		NSLog( @"%@", dateText );

		id tokens_0 = [ dateText componentsSeparatedByString: @"-" ];
		id tokens = [ NSMutableArray array ];
		id token;
		NSEnumerator* e = [ tokens_0 objectEnumerator ];
		while ( token = [ e nextObject ] ) {
			[ tokens addObjectsFromArray: [ token componentsSeparatedByString: @" " ] ];
		}
		for ( int i = 0; i < [ tokens count ]; i++ ) {
			token = [ tokens objectAtIndex: i ];
			if ( [ token isEqualToString: @"" ] ) {
				[ tokens removeObject: token ];
				i--;
			}
		}
//		NSLog( @"%@", tokens );

		if ( [ tokens count ] < 3 ) {
			NSLog( @"fail parse date" );
			[ results removeAllObjects ];
			return NO;
		}
		id year = [ tokens objectAtIndex: 0 ];	
		id month = [ tokens objectAtIndex: 1 ];
		id date = [ tokens objectAtIndex: 2 ];
	
		if ( [ month isEqualToString: @"Jan" ] ) {
			month = @"1";
		} else if ( [ month isEqualToString: @"Feb" ] ) {
			month = @"2";
		} else if ( [ month isEqualToString: @"Mar" ] ) {
			month = @"3";
		} else if ( [ month isEqualToString: @"Apr" ] ) {
			month = @"4";
		} else if ( [ month isEqualToString: @"May" ] ) {
			month = @"5";
		} else if ( [ month isEqualToString: @"Jun" ] ) {
			month = @"6";
		} else if ( [ month isEqualToString: @"Jul" ] ) {
			month = @"7";
		} else if ( [ month isEqualToString: @"Aug" ] ) {
			month = @"8";
		} else if ( [ month isEqualToString: @"Sep" ] ) {
			month = @"9";
		} else if ( [ month isEqualToString: @"Oct" ] ) {
			month = @"10";
		} else if ( [ month isEqualToString: @"Nov" ] ) {
			month = @"11";
		} else if ( [ month isEqualToString: @"Dec" ] ) {
			month = @"12";
		} else {
			NSLog( @"fail parse date" );
			[ results removeAllObjects ];
			return NO;
		}
		
		id firstDate = 
			[ NSDate dateWithNaturalLanguageString:
				[ NSString stringWithFormat: @"%@-%@-%@-00:00:00+09", year, month, date ] ];
//		NSLog( @"%@, %@, %@, %@", firstDate, year, month, date );

		thisNodes = [ node nodesForXPath: @"td[@class=\"B3\"]" error: & error ];
		for ( int i = 0; i < [ thisNodes count ]; i++ ) {
			thisNode = [ thisNodes objectAtIndex: i ];
			id date = [ firstDate dateByAddingYears: 0 months: 0 days: i hours: 0 minutes: 0  seconds: 0 ];
			id value  = [ thisNode stringValue ];
			value = [ value stringByTrimmingCharactersInSet: [ NSCharacterSet whitespaceAndNewlineCharacterSet ] ];
			if ( [ value isEqualToString: @"" ] ) {
				continue;
			}
			if ( latestDate != nil && [ date timeIntervalSinceDate: latestDate ] <= 0 ) {
				continue;
			}

			[ results setObject: value forKey: date ];
		}
	}
//	NSLog( @"%d", [ results count ] );
//	NSLog( @"%@", results );

	return YES;
}


@end
